/**
 * 报修记录表js
 */

$(function () {
    vm.getHospitals();
    vm.getResolvingStatus();
    vm.getRepairStatus();
	initialPage();
	getGrid();
});

function initialPage() {
	$(window).resize(function() {
		$('#dataGrid').bootstrapTable('resetView', {height: $(window).height()-56});
	});
}

function getGrid() {
	$('#dataGrid').bootstrapTableEx({
		url: '../../project/repair/list?_' + $.now(),
		height: $(window).height()-56,
		queryParams: function(params){
			params.name = vm.keyword;
			return params;
		},
		columns: [
			{checkbox: true},
            {field : "faultDetails", title : "故障信息", width : "350px"},
            {field : "hospitalName", title : "医院", width : "250px"},
            {field : "department", title : "科室", width : "100px"},
            {field : "reporter", title : "报修人", width : "100px"},
            {field : "repairStatusName", title : "报修状态", width : "100px"},
            {field : "resolvingStatusName", title : "解决状态", width : "100px"},
            {field : "operationMaintenancePerson", title : "运维人员", width : "100px"},
            {field : "repairTime", title : "报修时间", width : "200px"},
            {title : "操作", formatter : function(value, row, index) {
                    var _html = '';
                    if (hasPermission('project:repair:edit')) {
                        _html += '<a href="javascript:;" onclick="vm.edit(\''+row.id+'\')" title="编辑"><i class="fa fa-pencil"></i></a>';
                    }
                    if (hasPermission('project:repair:remove')) {
                        _html += '<a href="javascript:;" onclick="vm.remove(false,\''+row.id+'\')" title="删除"><i class="fa fa-trash-o"></i></a>';
                    }
                    _html += '<a href="javascript:;" onclick="vm.downloadAttachment(\'' + row.attachment + '\')" title="下载附件"><i class="fa fa-download"></i></a>';
                    return _html;
                }
            }
		]
	})
}

var vm = new Vue({
	el:'#dpLTE',
	data: {
		keyword: "",
		keyword1: "",
		keyword2: "",
		keyword3: "",
        optionsHospital: [],
        optionsResolvingStatus: [],
        optionsRepairStatus: [],
	},
	methods : {
		load: function(type) {
		    vm.keyword = vm.keyword3;
		    if (type == 1){
		        vm.keyword = vm.keyword1;
            } else if(type == 2) {
		        vm.keyword = vm.keyword2;
            }
            getGrid();
			$('#dataGrid').bootstrapTable('refresh');
		},
		save: function() {
			dialogOpen({
				title: '新增报修记录',
				url: 'base/repair/add.html?_' + $.now(),
                width: '800px',
                height: '620px',
                scroll: true,
                yes : function(iframeId) {
                    top.frames[iframeId].vm.acceptClick();
                },
			});
		},
		edit: function(id) {
            dialogOpen({
                title: '编辑报修记录',
                url: 'base/repair/edit.html?_' + $.now(),
                width: '800px',
                height: '620px',
                scroll: true,
                success: function(iframeId){
                    top.frames[iframeId].vm.projectRepairRecord.id = id;
                    top.frames[iframeId].vm.setForm();
                },
                yes: function(iframeId){
                    top.frames[iframeId].vm.acceptClick();
                }
            });
        },
        remove: function(batch, id) {
            var ids = [];
            if (batch) {
                var ck = $('#dataGrid').bootstrapTable('getSelections');
                if (!checkedArray(ck)) {
                    return false;
                }
                $.each(ck, function(idx, item){
                    ids[idx] = item.id;
                });
            } else {
                ids.push(id);
            }
            $.RemoveForm({
                url: '../../project/repair/remove?_' + $.now(),
                param: ids,
                success: function(data) {
                    vm.load();
                }
            });
        },
        downloadAttachment: function (attachment) {
            $.DownLoanForm({
                param: attachment,
            });
        },
        getHospitals: function () {
            $.getJSON("/sys/macro/value?_" + $.now() + "&value=客户", function (r) {
                vm.optionsHospital = r;
            });
        },
        getResolvingStatus: function () {
            $.getJSON("/sys/macro/value?_" + $.now() + "&value=解决状态", function (r) {
                vm.optionsResolvingStatus = r;
            });
        },
        getRepairStatus: function () {
            $.getJSON("/sys/macro/value?_" + $.now() + "&value=报修状态", function (r) {
                vm.optionsRepairStatus = r;
            });
        },
	}
});
